package ru.amse.tsyganov.jumleditor.view;

import java.util.Collections;
import java.util.List;

import org.eclipse.swt.graphics.GC;

import ru.amse.tsyganov.jumleditor.model.ActivityGraph;
import ru.amse.tsyganov.jumleditor.model.ForkJoinState;
import ru.amse.tsyganov.jumleditor.properties.EnumProperty;
import ru.amse.tsyganov.jumleditor.properties.StringProperty;

public class ForkJoinStateView<T extends ForkJoinState> extends StateView<T> {
	
	private static final int FORK_JOIN_WIDTH = 40;
	private static final int FORK_JOIN_HEIGHT = 10;
	
	private final List<ActivePoint> points;

	public ForkJoinStateView(T model, 
			ActivityGraphView<? extends ActivityGraph> activityGraphView) {
		
		super(model, activityGraphView);
		points = createNominalActivePoints();
		initProperties();
		setWidth(FORK_JOIN_WIDTH);
		setHeight(FORK_JOIN_HEIGHT);
	}

	@Override
	public void paintFigure(GC gc) {
		assert gc != null;
		
		gc.setBackground(getValidity().getColor());
		
		gc.fillRectangle(getRectangle());
	}
	
	@Override
	public void prepaintEvent(GC gc) {
	}

	@Override
	public List<? extends ActivePoint> getActivePoints() {
		if (getSelectedItems().selectedStatesContains(this)) {
			return points;
		} else {
			return Collections.emptyList();
		}
	}
	
	private void initProperties() {
		String[] elements = {"Vertical", "Horizontal"};
		
		super.getProperties().addProperty(
				new EnumProperty("Position", elements) {

			@Override
			public Integer getPropertyValue() {
				return (getWidth() > getHeight()) ? 1 : 0;
			}

			@Override
			public void setPropertyValue(Integer newValue) {
				int maxDim = Math.max(getWidth(), getHeight());
				int minDim = Math.min(getWidth(), getHeight());
				
				if (newValue == 0) {
					setWidth(minDim);
					setHeight(maxDim);
				} else {
					setWidth(maxDim);
					setHeight(minDim);
				}
			}

		});
	}
	
	@Override
	public StringProperty getMainProperty() {
		return null;
	}
	
}
